package com.wing.cabinet.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.StartSysSettingAdvert;
import com.wing.cabinet.service.StartSysSettingAdvertService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.setting.model.entity.SysSetting;
import com.wing.setting.model.response.SysSettingVO;
import com.wing.setting.service.SysSettingService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 柜面面板 德昌启动广告
 * @author
 */
@Validated
@Api(value = "Admin-刀具柜系统设置管理1", tags = "#/cabinet/startSetting")
@RestController("startSettingController")
@RequestMapping("cabinet/admin/startSetting")
@RequiredArgsConstructor
public class StartSettingController {

    private final SysSettingService startSysSettingService;

    @Autowired
    private StartSysSettingAdvertService advertService;

    /**
     * 柜子启动画面 admin
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @ApiOperation(value = "柜子启动画面", notes = "搜索")
    @GetMapping("/list")
    public PageResult<SysSetting> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            ,@RequestParam(value = "enterpriseId",required = false)Long enterpriseId
    ) {

        Page<SysSetting> pg = new Page<>(page, limit);
        QueryWrapper<SysSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",2);

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("desc", order)) {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }
        //搜索
        if (enterpriseId!=null && enterpriseId>0){
            queryWrapper.eq("enterprise_code",JwtUtils.getEnterpriseCode());
        }
        IPage<SysSetting> list = startSysSettingService.getBaseMapper().selectPage(pg, queryWrapper);
        for (int i = 0; i < list.getRecords().size(); i++) {
            list.getRecords().get(i).setEnterpriseName(JwtUtils.getEnterpriseName());
        }
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
    /**
     * 根据id查询详情
     */
    @GetMapping("/getAdvertDetail")
    public JsonResult getAdvertDetail(
             @RequestParam("id")Long id
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order){
        QueryWrapper<StartSysSettingAdvert> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setting_id",id);
        return JsonResult.success(advertService.list(queryWrapper));
    }

    /**
     * 添加数据
     */
    @PostMapping("/addSettingList")
    public JsonResult addSettingList(@RequestBody SysSettingVO sysSettingVO){

        return startSysSettingService.addSettingList(sysSettingVO);
    }
}
